/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jsepc.rep.logs;

import cn.com.jsepc.appframe.system.domain.User;
import cn.com.jsepc.rep.appframe.AppframeProxyService;
import cn.com.jsepc.rep.comm.Common;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.LogsVO;

import com.jarp.db.DAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author 邢妙珍
 */
public class LogsInfoManage {
     //加载数据
    public String getLogsGridData(String start, String limit, String KeyWord,String starttime,String endtime) {

    	//AppframeProxyService app=AppframeProxyService.getAppframeProxyService();
    	   System.out.println("starttime:"+starttime);
       String res = "";
        DAO dao = new DAO();
        try {
            List<LogsVO> lstResults = new ArrayList<LogsVO>();
            int iStart = Integer.parseInt(start);
            int iPageSize = Integer.parseInt(limit);
            int iCurPage = iStart / iPageSize + 1;
            StringBuffer sqlN = new StringBuffer();
            sqlN.append("select c.*,d.repname from (select a.*,b.text from tb_log a left join tb_log_rel b on a.type=b.logid)c left join tb_report d on c.repid=d.repid where 1=1");
            if (KeyWord != null && !KeyWord.equals("")) {
                sqlN.append(" and d.repname like '%" + KeyWord + "%'");
            }
            if (starttime != null && !starttime.equals("")) {
                sqlN.append(" and c.SEARCHTIME>=" + starttime);
            }
            if (endtime != null && !endtime.equals("")) {
                sqlN.append(" and c.SEARCHTIME<=" + endtime);
            }
            System.out.println("sqlN:"+sqlN.toString());
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sqlN.toString(), iCurPage, iPageSize);
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    LogsVO logsVO = new LogsVO();
                    logsVO.setId((String) hashRows.get("ID"));
                    logsVO.setLog((String) hashRows.get("LOG"));
                    logsVO.setRepid((String) hashRows.get("REPID"));
                    logsVO.setRepname((String) hashRows.get("REPNAME"));
                //    User user=new User();
                  //  user=app.getUserById(Long.valueOf( (String)hashRows.get("ROLEID")));
                    logsVO.setRoleid( (String)hashRows.get("ROLEID"));
                    logsVO.setTime((String) hashRows.get("TIME"));
                    logsVO.setType((String) hashRows.get("TYPE"));
                    logsVO.setText((String) hashRows.get("TEXT"));
                    lstResults.add(logsVO);
                }
            }
            JSONArray json = new JSONArray();
            StringBuffer str1 = new StringBuffer();
            str1.append("select count(1) from (select a.*,b.text from tb_log a left join tb_log_rel b on a.type=b.logid)c left join tb_report d on c.repid=d.repid where 1=1");
            if (KeyWord != null && !KeyWord.equals("")) {
                str1.append(" and d.repname like '%" + KeyWord + "%'");
            }
            if (starttime != null && !starttime.equals("")) {
                sqlN.append(" and c.SEARCHTIME>=" + starttime);
            }
            if (endtime != null && !endtime.equals("")) {
                sqlN.append(" and c.SEARCHTIME<=" + endtime);
            }
            String totalCount = dao.getDataValue(ConstUtil.DBNAME, str1.toString());
            json = JSONArray.fromObject(lstResults);
            res = "{totalCount:" + totalCount + ",data:" + json.toString() + "}";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }
}
